﻿/**
 * jQuery ligerUI 1.1.9
 * 
 * http://ligerui.com
 *  
 * Author franky 2012 [ ffychina@163.com ] 
 * 
 */
(function($){
  $.fn.ligerKindEditor=function(){
    return $.ligerui.run.call(this,"ligerKindEditor",arguments);
  };
  
  $.fn.ligerKindEditorManager=function(){
    return $.ligerui.run.call(this,"ligerKindEditorManager",arguments);
  };
  
  $.ligerDefaults.KindEditor={
    onChangeValue:null,
    width:null,
    disabled:false,
    value:null, //初始化值 
    nullText:null, //不能为空时的提示
    number:false
    //是否限定为浮点数格式输入框
  };
  
  $.ligerui.controls.KindEditor=function(element,options){
    $.ligerui.controls.KindEditor.base.constructor.call(this,element,options);
  };
  
  $.ligerui.controls.KindEditor.ligerExtend($.ligerui.controls.Input,{
    __getType:function(){
      return 'KindEditor'
    },
    __idPrev:function(){
      return 'KindEditor';
    },
    _init:function(){
      $.ligerui.controls.KindEditor.base._init.call(this);
      var g=this,p=this.options;
    },
    _render:function(){
      var g=this,p=this.options;
      g.inputText=$(this.element);
      //外层
      g.wrapper=g.inputText.wrap('<div class="l-kindeditor-wrap" style="width:auto;height:auto"></div>').parent();
      g.set(p);
      g.checkValue();
      var keoptions=$.extend({
        resizeType:2,
        minWidth:670,
        allowPreviewEmoticons : false,
        allowImageUpload : true,
        fileManagerJson:KindEditor.basePath+"file_manager_json.jsp"
        /*afterChange:function(){
          g.sync();
        }*/
      },p);
      delete(keoptions.options);
      g.ke=KindEditor.create('textarea[name="'+p.name+'"]',keoptions);
    },
    _getValue:function(){
      if(this.ke){
        return this.ke.html();
      }
      return this.inputText.val();
    },
    _setNullText:function(){
      this.checkNotNull();
    },
    checkValue:function(){
      var g=this,p=this.options;
      var v=g.inputText.val();
      if(p.number&&!/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(v)&&!/^\d+$/.test(v)){
        g.inputText.val(g.value||0);
        return;
      }
      g.value=v;
    },
    checkNotNull:function(){
      var g=this,p=this.options;
      if(p.nullText&&!p.disabled){
        if(!g.inputText.val()){
          g.inputText.addClass("l-textarea-field-null").val(p.nullText);
        }
      }
    },
    _setDisabled:function(value){
      if(this.ke){
        if(value)
          this.ke.disable();
        else
          this.ke.enable();
      }
    },
    _setWidth:function(value){
      if(this.ke)
        this.ke.setWidth(value);
    },
    _setHeight:function(value){
      if(this.ke)
        this.ke.setHeight(value);
    },
    _setValue:function(value){
      if(this.ke){
        this.ke.html(value||'');
      }
      this.inputText.val(value||'');
    },
    _setLabel:function(value){
      var g=this,p=this.options;
      if(!g.labelwrapper){
        g.labelwrapper=g.wrapper.wrap('<div class="l-labeltext"></div>').parent();
        var lable=$('<div class="l-textarea-label" style="float:left;">'+value+':&nbsp</div>');
        g.labelwrapper.prepend(lable);
        g.wrapper.css('float','left');
        if(!p.labelWidth){
          p.labelWidth=lable.width();
        }else{
          g._setLabelWidth(p.labelWidth);
        }
        lable.height(g.wrapper.height());
        if(p.labelAlign){
          g._setLabelAlign(p.labelAlign);
        }
        g.labelwrapper.append('<br style="clear:both;" />');
        g.labelwrapper.width(p.labelWidth+p.width+2);
      }else{
        g.labelwrapper.find(".l-textarea-label").html(value+':&nbsp');
      }
    },
    _setLabelWidth:function(value){
      var g=this,p=this.options;
      if(!g.labelwrapper)
        return;
      g.labelwrapper.find(".l-textarea-label").width(value);
    },
    _setLabelAlign:function(value){
      var g=this,p=this.options;
      if(!g.labelwrapper)
        return;
      g.labelwrapper.find(".l-textarea-label").css('text-align',value);
    },
    updateStyle:function(){
      this.constructor.base.updateStyle.call(this);
      var g=this,p=this.options;
      if(g.inputText.attr('disabled')||g.inputText.attr('readonly')){
        g.wrapper.addClass("l-textarea-disabled");
        g.options.disabled=true;
      }else{
        g.wrapper.removeClass("l-textarea-disabled");
        g.options.disabled=false;
      }
      if(g.inputText.hasClass("l-textarea-field-null")&&g.inputText.val()!=p.nullText){
        g.inputText.removeClass("l-textarea-field-null");
      }
      g.checkValue();
    }
  });
})(jQuery);